#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/select.h>

#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main() {
    int masterfd = open("/dev/ptmx", O_RDWR);
    char *name = ptsname(masterfd);

    int childfd;

    if (0 == (childfd = fork())) {
        setsid();
        int slavefd = open(name, O_RDWR);
        close(slavefd);
        close(masterfd);

        char *arg[] = {"top", 0};
        execvp(arg[0], arg);
        perror("exe failed.\r\n");
        exit(1);
    }

    int status = 0;
    waitpid(childfd, &status, 0);

    printf("child process end.\r\n");
    return 0;
}
